package com.instalinks.android.smspostservice.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.instalinks.android.smspostservice.AppTabBaseActivity;
import com.instalinks.android.smspostservice.R;
import com.instalinks.android.smspostservice.R.id;
import com.instalinks.android.smspostservice.utils.enums.TimeOuts;

public class NotificationsActivity extends AppTabBaseActivity {

	private Handler mHandler;
	private LinearLayout layoutNotifications;
	private Button btnC2dmRegister;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Intent intent = getIntent();
		final String action = intent.getAction();
		if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
			setupNotificationShortcut();

			if (!mAppCfg.isServiceRunning()) {
				this.appNotifications.notifyClearAll();
			}
			this.finish();
			return;
		} else {
			this.initialize();
		}
	}

	private void setupNotificationShortcut() {
		Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
		String shortcutName = "";
		Parcelable iconResource;

		shortcutIntent.setClassName(this, this.getClass().getName());
		shortcutIntent.putExtra(this.getClass().getName(),
				"Thin Server Provided This Shortcut");
		shortcutName = "Notifications";
		iconResource = Intent.ShortcutIconResource.fromContext(this,
				R.drawable.notification);

		// Then, set up the container intent (the response to the caller)

		Intent intent = new Intent();
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

		// Now, return the result to the launcher

		if (this.mAppCfg.isSubscribedFlag())
			setResult(RESULT_OK, intent);
		else
			setResult(RESULT_CANCELED, intent);
	}

	private void initialize() {
		this.setContentView(R.layout.notification);

		layoutNotifications = (LinearLayout) this
				.findViewById(R.id.layoutNotifications);

		btnC2dmRegister = (Button) findViewById(id.btnC2dmRegister);

		btnC2dmRegister.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					mAppCfg.setRegistrationId("");
					mAppCfg.registerForC2dm();
				} catch (Exception e) {
					mAppCfg.alert("Error performing registration : "
							+ e.getMessage());
				}
			}
		});

		mHandler = new Handler();
		mHandler.removeCallbacks(statusUpdater);
		mHandler.post(statusUpdater);

		// setValues();
	}

	private void addAttribute(String notificationText) {
		LayoutInflater li = (LayoutInflater) getApplicationContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = li.inflate(R.layout.notification_item, null);

		// v.setTag(notificationText);
		((TextView) v.findViewById(R.id.txtNotification))
				.setText(notificationText);

		layoutNotifications.addView(v, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
	}

	private void setValues() {
		mAppCfg.rePopulate();

		layoutNotifications.removeAllViews();

		int size = mAppCfg.getNotificationQueueSize();

		for (int i = size - 1; i >= 0; i--) {

			String val = mAppCfg.getNotificationFromQueue(i);
			val = val.replaceFirst(";", ": ");
			addAttribute(val);
		}
	}

	private Runnable statusUpdater = new Runnable() {

		@Override
		public void run() {
			setValues();
			mHandler.postDelayed(this, TimeOuts.TwoSec.getValue());
		}
	};

	@Override
	protected boolean doBackgroundWork() {
		return false;
	}

	@Override
	protected void doOnSuccess() {
	}

	@Override
	protected void doOnFailure() {
		// TODO Auto-generated method stub

	}
}
